package com.igd.business.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.igd.business.dao.IRoleDao;
import com.igd.common.client.IbatisCallBack;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.PageList;
import com.igd.security.RoleResource;
import com.igd.security.Roles;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-7-11
 *
 * @description	 角色信息Dao接口实现类
 */

public class IRoleDaoImpl extends IBaseDaoImpl implements IRoleDao {
	
	private final String GET_ROLES_PAGELIST_SQL = "roles.queryAllRole";
	
	private final String GET_ROLES_PAGELIST_COUNT_SQL = "roles.queryRoleCount";
	
	private final String DELETE_ROLE_INFO_SQL = "roles.delRoleInfo";
	
	private final String UPDATE_ROLE_INFO_SQL = "roles.updateRole";
	
	private final String INSERT_ROLE_INFO_SQL = "roles.insertRole";
	
	private final String GET_ROLE_INFO_SQL = "roles.queryRole";
	
	private final String GET_ROLE_RESOURCE_PAGELIST_SQL = "roles.queryRoleResource";
	
	private final String GET_ROLE_RESOURCE_PAGELIST_COUNT_SQL = "roles.queryRoleResourceCount";
	
	private final String DELETE_ROLE_RESOURCE_INFO_SQL = "roles.delRoleResourceInfo";
	
	private final String GET_MAX_ROLEID_SQL = "roles.getMaxRolePrimaryKey";
	
	private final String INSERT_ROLE_RESOURCE_SQL = "roles.insertRoleResource";
	
	public List<RoleResource> queryRoleResource(PageList pageList,RoleResource roleResource) throws SQLException, IgdException {
		
		return this.queryForListPage(GET_ROLE_RESOURCE_PAGELIST_SQL, GET_ROLE_RESOURCE_PAGELIST_COUNT_SQL, pageList, roleResource);
	}
	
	public List<Roles> queryAllRole(PageList pageList, Roles role) throws SQLException, IgdException {
		
		return this.queryForListPage(GET_ROLES_PAGELIST_SQL, GET_ROLES_PAGELIST_COUNT_SQL, pageList, role);

	}

	public void deleteRole(Roles role) throws SQLException, IgdException {
		
		this.delete(DELETE_ROLE_INFO_SQL, role);

	}

	public void updRole(Roles role) throws SQLException, IgdException {
		
		this.update(UPDATE_ROLE_INFO_SQL, role);
	}

	public Roles insertRole(Roles role) throws SQLException, IgdException {
		
		role.setRolesId(this.generatePrimaryKey(GET_MAX_ROLEID_SQL));
		
		this.save(INSERT_ROLE_INFO_SQL,role);
		
		return role;
	}

	public boolean batchInsertRoleResource(final List<RoleResource> roleRes) throws IgdException {
		
		boolean success = Boolean.TRUE;
		
		success = this.batchExecute(INSERT_ROLE_RESOURCE_SQL,roleRes,new IbatisCallBack() {
			
			public Object execute(SqlMapExecutor executor) throws SQLException {
				
				int roleResSize = roleRes.size();
				
				int processCount = 0;
				
				executor.startBatch();
				
				for(RoleResource res : roleRes){

					executor.insert(INSERT_ROLE_RESOURCE_SQL,res);

					processCount ++;

				}
				
				executor.executeBatch();
				
				return processCount == roleResSize ? Boolean.TRUE : Boolean.FALSE;
			}
		});
		
		return success;
	}

	public Roles queryRoleInfo(Roles role) throws SQLException, IgdException {
		
		return this.queryForObject(GET_ROLE_INFO_SQL, role);
	}

	public void deleteRoleResource(RoleResource roleRes) throws SQLException,IgdException {
		
		this.delete(DELETE_ROLE_RESOURCE_INFO_SQL,roleRes);
	}
}
